package com.prs.crm.dao.cdxh.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.courser.hibernate.QueryExample;
import org.courser.ui.PaginationSupport;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cdxh.cd_ProductDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.domain.cdxh.cd_ProductClass;
import com.prs.crm.domain.cdxh.material.MaterialTeam;

public class cd_ProductDaoImpl extends BaseDaoImpl<cd_Product> implements
		cd_ProductDao {

	@Override
	public List<cd_Product> getCdProducts() {
		return  (List<cd_Product>) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session.createCriteria(cd_Product.class);
						return criteria.list();
					}
				});
	}

	@Override
	public Collection<cd_Product> getCdProductsById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public cd_Product getProductObject(final String productCode) {
		return (cd_Product)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(cd_Product.class);
				        	criteria.add(Restrictions.eq("productcode", productCode));		        	
				        	List<cd_Product> result = criteria.list();
				        	
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}

	@Override
	public void saveOrProduct(cd_Product cd_Product) {
     	this.getHibernateTemplate().saveOrUpdate(cd_Product);
	}

	@Override
	public cd_Product getCdProductById(final int id) {
		return (cd_Product)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(cd_Product.class);
				        	criteria.add(Restrictions.eq("id", id));		        	
				        	List<cd_Product> result = criteria.list();
				        	
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}

	
}
